<?php

use Phalcon\Mvc\Controller;

class ControllerBase extends Controller
{
    function getGlobal(){
        //全局设置
        $settingResult = Setting::find();
        foreach ($settingResult as $value) {
            $setting[$value->keyword] = $value->value;
        }
        $setting = (object)$setting;

        //导航
        $nav_category_id = $setting->nav_category_id;



        //一级导航
        $navResultSource = Link::find([
            "category_id = $nav_category_id AND is_visible = 1 AND is_delete = 0",
            "order"=>"weight DESC, created_at DESC"
            ]);

        foreach ($navResultSource as $value) {
            $navResult[] =  $value;
        }

        //二级导航
        $navCatResult = Category::find([
                "father_id = $nav_category_id AND is_visible = 1 AND is_delete = 0",
                "order"=>"weight DESC, id ASC"
                ]);
        if(isset($navCatResult[0])){
            foreach ($navCatResult as $navCat) {
                $nav_category_id = $navCat->id;
                $linkResult = Link::find([
                    "category_id = $nav_category_id AND is_visible = 1 AND is_delete = 0",
                    "order"=>"weight DESC, created_at DESC"
                    ]);
                $navResult[] = (object)[
                    "title"     => $navCat->name,
                    "url"       => isset($linkResult[0])?$linkResult[0]->url:"#",
                    "sons"      => $linkResult
                ];
            }
        }
        
        //友情链接
        $favolink_category_id = $setting->favolink_category_id;
        $favolinkResult = Link::find([
                "category_id = $favolink_category_id AND is_visible = 1 AND is_delete = 0",
                "order"=>"weight DESC, created_at DESC"
                ]);
        
   
        $this->view->setVar("nav",      $navResult);
        $this->view->setVar("favolink", $favolinkResult);
        $this->view->setVar("setting",  $setting);
    }
	
}
